package sdc.media.motorbike.View;

import sdc.media.motorbike.MotorBike;
import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class SelectMotorScreen extends BaseScreen {

	// Background and button, etc...
	private Image imgBack;
	private Image buttonLeft;
	private Image buttonRight;
	private Image buttonSelectMotor;
	
	private boolean isLeftClick;
	private boolean isRightClick;
	private boolean isSelectMotorClick;
	
	private float sizeX;
	private float sizeY;

	private int select = 1;
	
	@Override
	public void render(float delta) {
		super.render(delta);
		batch.begin();
		
		if(select > 1) {
			if(isLeftClick)
				batch.draw(Assets.left_click, 30 * sizeX, 305 * sizeY, Assets.left_click.getRegionWidth() * sizeX, Assets.left_click.getRegionHeight() * sizeY);
			else
				batch.draw(Assets.left, 30 * sizeX, 305 * sizeY, Assets.left.getRegionWidth() * sizeX, Assets.left.getRegionHeight() * sizeY);
		}
		
		if( select < 3) {
			if(isRightClick && select < 3)
				batch.draw(Assets.right_click, 420 * sizeX, 305 * sizeY, Assets.right_click.getRegionWidth() * sizeX, Assets.right_click.getRegionHeight() * sizeY);
			else
				batch.draw(Assets.right, 420 * sizeX, 305 * sizeY, Assets.right.getRegionWidth() * sizeX, Assets.right.getRegionHeight() * sizeY);
		}
		
		if(select == 1)
			batch.draw(Assets.select_1, 80 * sizeX, 350 * sizeY, Assets.select_1.getRegionWidth() * sizeX, Assets.select_1.getRegionHeight() * sizeY);
		else if(select == 2)
			batch.draw(Assets.select_2, 50 * sizeX, 350 * sizeY, Assets.select_2.getRegionWidth() * sizeX, Assets.select_2.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.select_3, 50 * sizeX, 350 * sizeY, Assets.select_3.getRegionWidth() * sizeX, Assets.select_3.getRegionHeight() * sizeY);
		
		if(!Setting.cars[select - 1]) {
			batch.draw(Assets.locked, 100 * sizeX, 450 * sizeY, Assets.locked.getRegionWidth() * sizeX, Assets.locked.getRegionHeight() * sizeY);
		} else {
			if(isSelectMotorClick)
				batch.draw(Assets.buttonSelectMotor_click, 80 * sizeX, 300 * sizeY, Assets.buttonSelectMotor_click.getRegionWidth() * sizeX, Assets.buttonSelectMotor_click.getRegionHeight() * sizeY);
			else
				batch.draw(Assets.buttonSelectMotor, 80 * sizeX, 300 * sizeY, Assets.buttonSelectMotor.getRegionWidth() * sizeX, Assets.buttonSelectMotor.getRegionHeight() * sizeY);
		}
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void show() {
		super.show();
		
		// Constructor
		sizeX = Gdx.graphics.getWidth() / 480.0f;
		sizeY = Gdx.graphics.getHeight() / 800.0f;
		
		imgBack = new Image(Assets.selectBackground);
		imgBack.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		stage.addActor(imgBack);
		
		buttonLeft = new Image();
		buttonLeft.setSize(Assets.left.getRegionWidth() * 2 * sizeX, Assets.left.getRegionHeight() * 2 * sizeY);
		buttonLeft.setPosition(30 * sizeX, 305 * sizeY);
		stage.addActor(buttonLeft);
		
		buttonRight = new Image();
		buttonRight.setSize(Assets.right.getRegionWidth() * 2 * sizeX, Assets.right.getRegionHeight() * 2 * sizeY);
		buttonRight.setPosition(420 * sizeX, 305 * sizeY);
		stage.addActor(buttonRight);
		
		buttonSelectMotor = new Image();
		buttonSelectMotor.setSize(Assets.buttonSelectMotor.getRegionWidth() * sizeX, Assets.buttonSelectMotor.getRegionHeight() * sizeY);
		buttonSelectMotor.setPosition(80 * sizeX, 300 * sizeY);
		stage.addActor(buttonSelectMotor);

		if (Setting.soundEnabled) {
			// Play music and sound
			Assets.backgroundPlayMusic.play();
		} else {
			//Pause music and sound
			Assets.backgroundPlayMusic.pause();
		}

		// Button Event...
		buttonLeft.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isLeftClick = false;
				if(select > 1) {
					select--;
					Assets.playSound(Assets.buttonClick);
				}
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				isLeftClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonRight.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isRightClick = false;
				if(select < 3) {
					select++;
					Assets.playSound(Assets.buttonClick);
				}
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				isRightClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonSelectMotor.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isSelectMotorClick = false;
				if(Setting.cars[select - 1]) {
					Assets.playSound(Assets.buttonClick);
					Setting.carType = select;
					MotorBike.getManagerScreen().createScreen(ManagerScreen.SCREEN_PLAY);
				}
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				isSelectMotorClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
	}

}
